
function Cargoscan(ply)
	for index, cargohold in pairs (servertable.ship.cargoholds) do
		if (cargohold:IsValid()) then
			if IsOwner(cargohold,ply) then
				cargohold:Scan()
			end
		end
	end
	for index, cargohold in pairs (servertable["ship"]["Looter"]) do
		if (cargohold:IsValid()) then
			if IsOwner(cargohold,ply) then
				cargohold:Scan()
			end
		else
			table.remove(servertable["ship"]["Looter"],index)
		end
	end
end
concommand.Add("Cargoscan",Cargoscan) 

function Cargostart(ply,command,args)
	local done = false
	for index, cargohold in pairs (servertable["ship"]["cargoholds"]) do
		if (cargohold:IsValid()) then
			if IsOwner(cargohold,ply) then
				local itemlist = {}
				local rp = RecipientFilter()
				rp:AddPlayer( ply )
				local itemlist = {}
				local amount = {}
				for k,class in pairs(cargohold.class) do 
					if 	not (table.HasValue(itemlist,class)) then
						table.insert(itemlist,class)
					else
						local amount2 = 0
						for _,class2 in pairs(cargohold.class) do 
							if (class == class2) then
								amount2 = amount2 + 1
							end
						end
						for posit,item2 in pairs(itemlist) do 
							if (item2 == class) then
								if (amount2 == 0) then 
									amount2 = 1 
								end
								amount[posit] = amount2
							end
						end
					end
				end
				for number, item in pairs (itemlist) do	
					umsg.Start("CargoSystem", rp) 
					umsg.String( item )
					if (amount[number] == 0) then
						amount[number] = 1
					end
					umsg.Long(amount[number])
					umsg.End() 
				end
				umsg.Start("CargoSystem", rp) 
				umsg.String( "Stop" )
				umsg.End() 
				done = true
				break
			end
		else
			table.remove(servertable["ship"]["cargoholds"][index],index)
		end
	end
	if not done then
		local rp = RecipientFilter()
		rp:AddPlayer( ply )
		ply:PrintMessage(HUD_PRINTCENTER, "You do not have a Cargohold" )
		umsg.Start("CargoSystem", rp) 
		umsg.String( "Stop" )
		umsg.End() 
	end
end
concommand.Add("CargoStart",Cargostart) 

function CargoRecive(ply,command,args)
local tradegood = args[1]
local mode = args[2]	
Msg("Ejecting" .. " " .. tradegood .. " " .. mode)
	for index, cargohold in pairs (servertable["ship"]["cargoholds"]) do
		if (cargohold:IsValid()) then
			if IsOwner(cargohold,ply) then
				cargohold.scan = false
				cargohold.ejecttype = tradegood
				if (mode == "all") then
					cargohold.ejectall = true
				elseif (mode == "one" ) then
					cargohold.ejectall = false
					cargohold:Eject()
					ply:ConCommand("CargoStart")
					return
				end
				cargohold:Eject()
			end	
		end
	end
	ply:ConCommand("CargoStart")
end
concommand.Add("CargoRecive",CargoRecive) 